home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / adt / char.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.1 KB  |  222 lines

  1. /*
  2.  * char.c --
  3.  *     Functions for the built-in type "char".
  4.  *     Functions for the built-in type "char16".
  5.  */
  6.  
  7. #include <strings.h>
  8.  
  9. #include "tmp/postgres.h"
  10.  
  11. RcsId("$Header: /private/postgres/src/utils/adt/RCS/char.c,v 1.13 1992/07/12 23:12:08 joey Exp $");
  12.  
  13. #include "utils/palloc.h"
  14.  
  15. char *PG_username;
  16.  
  17. char *
  18. pg_username()
  19. {
  20.     return(PG_username);
  21. }
  22.  
  23.         /* ========== USER I/O ROUTINES ========== */
  24.  
  25.  
  26. /*
  27.  *    charin        - converts "x" to 'x'
  28.  */
  29. int32
  30. charin(ch)
  31.     char    *ch;
  32. {
  33.     if (ch == NULL)
  34.         return((int32) NULL);
  35.     return((int32) *ch);
  36. }
  37.  
  38. /*
  39.  *    charout        - converts 'x' to "x"
  40.  */
  41. char *
  42. charout(ch)
  43.     int32    ch;
  44. {
  45.     char    *result = (char *) palloc(2);
  46.  
  47.     result[0] = (char) ch;
  48.     result[1] = '\0';
  49.     return(result);
  50. }
  51.  
  52. /*
  53.  *    cidin    - converts "..." to internal representation.
  54.  *
  55.  *     NOTE: we must no use 'charin' because cid might be a non
  56.  *    printable character...
  57.  */
  58. int32
  59. cidin(s)
  60. char *s;
  61. {
  62.     CommandId c;
  63.  
  64.     if (s==NULL)
  65.     c = 0;
  66.     else
  67.     c = atoi(s);
  68.  
  69.     return((int32)c);
  70. }
  71.  
  72. /*
  73.  *    cidout    - converts a cid to "..."
  74.  *
  75.  *     NOTE: we must no use 'charout' because cid might be a non
  76.  *    printable character...
  77.  */
  78. char *
  79. cidout(c)
  80. int32 c;
  81. {
  82.     char *result;
  83.     CommandId c2;
  84.  
  85.     /*
  86.      * cid is a number between 0 .. 2^16-1, therefore we need at most
  87.      * 4 chars for the string (6 digits + '\0')
  88.      * NOTE: print it as an UNSIGNED int!
  89.      */
  90.     result = palloc(8);
  91.     c2 = (CommandId)c;
  92.     sprintf(result, "%u", (unsigned)(c2));
  93.     return(result);
  94. }
  95.  
  96. /*
  97.  *    char16in    - converts "..." to internal reprsentation
  98.  *
  99.  *    Note:
  100.  *        Currently if strlen(s) < 14, the extra chars are nulls
  101.  */
  102. char *
  103. char16in(s)
  104.     char    *s;
  105. {
  106.     char    *result;
  107.     int      i;
  108.     if (s == NULL)
  109.         return(NULL);
  110.     result = (char *) palloc(16);
  111.     strncpy(result, s, 16);
  112.     /* null out the extra chars */
  113.     for (i = strlen(s) + 1; i < 16; i++)
  114.       result[i] = '\0';
  115.     return(result);
  116. }
  117.  
  118. /*
  119.  *    char16out    - converts internal reprsentation to "..."
  120.  */
  121. char *
  122. char16out(s)
  123.     char    *s;
  124. {
  125.     char    *result = (char *) palloc(17);
  126.  
  127.     if (s == NULL) {
  128.         result[0] = '-';
  129.         result[1] = '\0';
  130.     } else {
  131.         strncpy(result, s, 16);
  132.         result[16] = '\0';
  133.     }
  134.     return(result);
  135. }
  136.  
  137.  
  138.          /* ========== PUBLIC ROUTINES ========== */
  139.  
  140. int32 chareq(arg1, arg2)    int8 arg1, arg2; { return(arg1 == arg2); }
  141. int32 charne(arg1, arg2)    int8 arg1, arg2; { return(arg1 != arg2); }
  142. int32 charlt(arg1, arg2)    int8 arg1, arg2; { return(arg1 < arg2); }
  143. int32 charle(arg1, arg2)    int8 arg1, arg2; { return(arg1 <= arg2); }
  144. int32 chargt(arg1, arg2)    int8 arg1, arg2; { return(arg1 > arg2); }
  145. int32 charge(arg1, arg2)    int8 arg1, arg2; { return(arg1 >= arg2); }
  146. int32 charpl(arg1, arg2)    int8 arg1, arg2; { return(arg1 + arg2); }
  147. int32 charmi(arg1, arg2)    int8 arg1, arg2; { return(arg1 - arg2); }
  148. int32 charmul(arg1, arg2)    int8 arg1, arg2; { return(arg1 * arg2); }
  149. int32 chardiv(arg1, arg2)    int8 arg1, arg2; { return(arg1 / arg2); }
  150.  
  151.  
  152. /*
  153.  *    char16eq    - returns 1 iff arguments are equal
  154.  *    char16ne    - returns 1 iff arguments are not equal
  155.  *
  156.  *    BUGS:
  157.  *        Assumes that "xy\0\0a" should be equal to "xy\0b".
  158.  *        If not, can do the comparison backwards for efficiency.
  159.  *
  160.  *    char16lt    - returns 1 iff a < b
  161.  *    char16le    - returns 1 iff a <= b
  162.  *    char16gt    - returns 1 iff a < b
  163.  *    char16ge    - returns 1 iff a <= b
  164.  *
  165.  */
  166. int32
  167. char16eq(arg1, arg2)
  168.     char    *arg1, *arg2;
  169. {
  170.  
  171.     if (arg1 == NULL || arg2 == NULL)
  172.     return((int32) NULL);
  173.     return((int32) (strncmp(arg1, arg2, 16) == 0));
  174. }
  175.  
  176. int32
  177. char16ne(arg1, arg2)
  178.     char    *arg1, *arg2;
  179. {
  180.     return((int32) !char16eq(arg1, arg2));
  181. }
  182.  
  183. int32
  184. char16lt(arg1, arg2)
  185.     char    *arg1, *arg2;
  186. {
  187.     if (arg1 == NULL || arg2 == NULL)
  188.     return((int32) NULL);
  189.  
  190.     return((int32) (strncmp(arg1, arg2, 16) < 0));
  191. }
  192.  
  193. int32
  194. char16le(arg1, arg2)
  195.     char    *arg1, *arg2;
  196. {
  197.     if (arg1 == NULL || arg2 == NULL)
  198.     return((int32) NULL);
  199.  
  200.     return((int32) (strncmp(arg1, arg2, 16) <= 0));
  201. }
  202.  
  203. int32
  204. char16gt(arg1, arg2)
  205.     char    *arg1, *arg2;
  206. {
  207.     if (arg1 == NULL || arg2 == NULL)
  208.     return((int32) NULL);
  209.  
  210.     return((int32) (strncmp(arg1, arg2, 16) > 0));
  211. }
  212.  
  213. int32
  214. char16ge(arg1, arg2)
  215.     char    *arg1, *arg2;
  216. {
  217.     if (arg1 == NULL || arg2 == NULL)
  218.     return((int32) NULL);
  219.  
  220.     return((int32) (strncmp(arg1, arg2, 16) >= 0));
  221. }
  222.